require(["./config"],function(){
  require(["jquery","loadHF"],function($){

    class Register{
      constructor(){
        this.init();
      }

      init(){
        this.register_birth=  $(".register_birth");
        this.usernameInput = $("#username");
        this.phoneInput=$("#phoneInput");
        this.passwordInput = $("#passwordInput");
        this.passwordInput2=$("#register_password2");
        this.register_btn=$("#register_btn");

        this.register_birth.on("click",function(event){
            let ul=$(event.target).siblings(".show_date");
            ul.css("display","block");
            ul.on("click",function(event){
              const text = $(event.target).text();
              console.log(text);
              $(event.target).parent().siblings(".register_date").val(text);
              $(event.target).parent().css("display","none");
          })
        });

        // 提交表单
        this.register_btn.on("click", ()=>{
          let username = this.usernameInput.val(),
            password = this.passwordInput.val(),
            password2 = this.passwordInput2.val(),
            phone = this.phoneInput.val();
          let reg1 = /^[\u4E00-\u9FA5A-Za-z0-9_]+$/;
          let reg2 = /^\w{6,}$/;
          let reg3= /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;

          // 判断输入格式是否正确
          if(!reg1.test(username)){
            alert("用户名格式错误");

          }else if (!reg2.test(password)){
            alert("请输入正确格式的密码（至少6位）");
          }else if(!reg3.test(phone)){
            alert("手机号格式不匹配");
          }else if(password!==password2){
              alert("两次密码输入不一致");
          }else {
            //ajax
            $.ajax({
              url: "http://localhost/etude_project/api/v1/register.php",
              method: "post",
              data: {username, password,phone},
              dataType: "json",
              success: function(data){
                console.log(data);
                if (data.res_body.status === 1) { // 注册成功
                  alert("登录成功");
                  location = "/html/login.html";
                } else { // 注册失败
                  alert("注册失败");
                }
              }
            })
          }

          // $.ajax({
          //   url: "http://localhost/etude_project/api/v1/register.php",
          //   method: "post",
          //   data: {username, password,phone},
          //   dataType: "json",
          //   success: function(data){
          //     console.log(data);
          //     if (data.res_body.status === 1) { // 注册成功
          //
          //       location = "/html/login.html";
          //     } else { // 注册失败
          //       alert("注册失败");
          //     }
          //   }
          // })
          // 阻止默认提交
          return false;
        })
      }

      }


      new Register();
  })
})
